package com.TripMate.system.AI;

import com.tencentcloudapi.common.CommonClient;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Token {
    private static final Logger logger = LoggerFactory.getLogger(Token.class);

    private static final String SECRET_ID = "AKIDDE1cz7JNJ6i8LJAxXu55tWxmaGm7HJBs"; // 填入腾讯云AKSK密钥(从腾讯云控制台获取)
    private static final String SECRET_KEY = "jKn7YyGNIGCzhEds7fFm7GICwZ2LZeZI"; // 填入腾讯云AKSK密钥(从腾讯云控制台获取)
    private static final int CONN_TYPE_API = 5; // API 访客
    private static final String BOT_APP_KEY = "zCoPERcp";  // 机器人密钥，不是BotBizId (从运营接口人处获取)
    private static final String VISITOR_BIZ_ID = "202403180001"; // 访客 ID（外部系统提供，需确认不同的访客使用不同的 ID）
    private static final String ROOT_DOMAIN = "tencentcloudapi.com";
    private static final String REQ_METHOD = HttpProfile.REQ_POST;
    private static final String PROTOCOL = HttpProfile.REQ_HTTPS;
    private static final String SERVICE_NAME = "lke";
    private static final String VERSION = "2023-11-30";
    private static final String REGION = "ap-guangzhou";
    private static final String ACTION = "GetWsToken";

    public String getWsToken() {
        try {
            // 创建凭据对象
            Credential cred = new Credential(SECRET_ID, SECRET_KEY);

            // 创建 HTTP 配置对象
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setRootDomain(ROOT_DOMAIN);
            httpProfile.setReqMethod(REQ_METHOD);
            httpProfile.setProtocol(PROTOCOL);

            // 创建客户端配置对象
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            // 创建客户端对象
            CommonClient client = new CommonClient(SERVICE_NAME, VERSION, cred, REGION, clientProfile);

            // 创建请求参数
            JSONObject params = new JSONObject();
            params.put("Type", CONN_TYPE_API);
            params.put("BotAppKey", BOT_APP_KEY);
            params.put("VisitorBizId", VISITOR_BIZ_ID);

            // 发送请求并获取响应
            String respJsonStr = client.call(ACTION, params.toString());

            // 解析响应
            JSONObject resp = new JSONObject(respJsonStr);
            JSONObject respData = resp.getJSONObject("Response");
            return respData.getString("Token");
        } catch (Exception e) {
            logger.error("Error occurred while getting token: {}", e.getMessage(), e);
            return "";
        }
    }
}
